package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.HisUser;
import com.his.entity.Operation;
import com.his.entity.Order;
import com.his.mapper.OperationMapper;
import com.his.service.OperationService;
import com.his.service.OrderService;
import com.his.utils.MyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Operation)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:13:46
 */
@Service("operationService")
public class OperationServiceImpl implements OperationService {
    @Resource
    private OperationMapper operationMapper;

    @Resource
    private OrderService orderService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Rsp queryById(Integer id) {
        Operation operation = this.operationMapper.queryById(id);
        if (StringUtils.isEmpty(operation)) {
            return Rsp.fail().msg("未找到当前数据");
        } else {
            return Rsp.success(operation);
        }
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param operation 实例对象
     * @return 查询的结果
     */
    @Override
    public List<Operation> queryAll(Operation operation) {
        return this.operationMapper.queryAll(operation);
    }

    /**
     * 新增数据
     *
     * @param operation 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(Operation operation) {
        //默认创建时间
        operation.setCreateTime(new Date());
        //默认手术结果
        operation.setOperationResult("未开始");
        //默认状态
        operation.setStatus("0");
        Integer insert = this.operationMapper.insert(operation);
        if (insert > 0) {
            return Rsp.success(null).msg("修改成功");
        } else {
            return Rsp.fail();
        }
    }

    /**
     * 修改数据
     *
     * @param operation 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(Operation operation) {
        int update = this.operationMapper.update(operation);
        if (update > 0) {
            return this.queryById(operation.getId()).msg("修改成功");
        } else {
            return Rsp.fail().msg("修改失败");
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.operationMapper.deleteById(id) > 0;
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public Rsp deleteByIdLogic(Integer id) {
        Integer deleteByIdLogic = operationMapper.deleteByIdLogic(id);
        if (deleteByIdLogic > 0) {
            return Rsp.success(null).msg("逻辑删除成功");
        } else {
            return Rsp.fail().msg("逻辑删除失败");
        }
    }

    /**
     * 分页查询
     *
     * @param pageParam
     * @return
     */
    @Override
    public PageInfo<Operation> searchSomeOperationPage(PageParam<Operation> pageParam, HisUser user) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        Operation model = pageParam.getModel();
        if(user.getRoles().contains("doctor")){
            model.setCreateId(user.getId());
        }
        return new PageInfo<>(operationMapper.queryAllByModel(model));
    }


    /**
     * 填写手术结果
     *
     * @param operation
     * @return
     */
    @Override
    //@Transactional(propagation = Propagation.REQUIRED)
    public Rsp endOne(String token, Operation operation) {
        //手术结束设置状态码为1
        operation.setStatus("1");
        int update = operationMapper.update(operation);
        if (update > 0) {
            //生成订单
            Order order = new Order();
            order.setPatientId(operation.getPatientId());//设置病人id
            order.setCategoryId(1);//设置状态码为结束
            try {
                order.setCreateUserId(MyUtil.getUserByToken(token));//设置创建者id
            } catch (Exception e) {
                e.printStackTrace();
            }
            order.setDescript(operation.getOperationContent());//订单简介
            order.setTotalMoney(operation.getOperationCost());//订单金额
            order.setCreateTime(new Date());//创建时间
            order.setStatus("0");//默认订单状态
            order.setIsDelete("0");//默认删除状态
            //写入订单
            Order insert = orderService.insert(order);
            return Rsp.success(operation).msg("编辑成功");
        } else {
            return Rsp.fail().msg("编辑失败");
        }
    }
}
